import formInfo from '@ohos.app.form.formInfo';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';

import { chaplog } from '../models/chaplog';
import { RankingServices } from '../services/RankingServices';
import formProvider from '@ohos.app.form.formProvider';
import agconnect from '@hw-agconnect/api-ohos';
import "@hw-agconnect/function-ohos";
import { AppStorageKit } from '../common/AppStorageKit';

import {PreferencesUtil} from '../common/PreferencesUtil';


export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want) {
    console.log('进入onAddForm');
    // Called to return a FormBindingData object.
    let formData = { };
    let pageIndex = 1;
    let pageSize =3;

    // AppStorageKit.GetLoginUser(this.context).then(user=>{
    //   console.log('onAddForm loginUser->', JSON.stringify(user));
    // });

    AppStorageKit.GetRank(this.context).then(ret=>{
      console.log('onAddForm ranking11->', JSON.stringify(ret[0]));

      ret.forEach(item=>{
        if(item.accoutName.includes('@')){
          item.accoutName = item.accoutName.split('@')[0];
        }else{
          item.accoutName = item.accoutName.slice(7,11);
        }
      })

      // 卡片id固定
      let formId = want.parameters["ohos.extra.param.key.form_identity"];

      // 将formId存到首选项
      // AppStorageKit.SetCarID(formId,this.context);
      PreferencesUtil.getInstance().addFormId(this.context,formId);
      console.log('formId===>',formId,JSON.stringify(this.context).slice(0,800));

      // 传入给卡片的数据
      let data = {
        first:{
          name: ret[0].accoutName,
          score: ret[0].score,
          avatar: ret[0].headImg
        },
        second:{
          name: ret[1].accoutName,
          score: ret[1].score,
          avatar: ret[1].headImg
        },
        third:{
          name: ret[2].accoutName,
          score: ret[2].score,
          avatar: ret[2].headImg
        }
      }
      //  将数据转为卡片接收的格式
      let formData = formBindingData.createFormBindingData(data);
      formProvider.updateForm(formId,formData).then((data) => {
        console.info('FormAbility updateForm success.' + JSON.stringify(data));
      }).catch((error) => {
        console.error('FormAbility updateForm failed: ' + JSON.stringify(error));
      })
    });

    /*
     *    agconnect.instance().init(this.context);
          let functionCallable = agconnect.function().wrap("idgenerator-$latest");
          functionCallable.timeout = 3000;
          // 不传参调用云函数
          console.log('call方法');
           functionCallable.call().then(ret=>{
             console.log('call执行');
            let returnValue = ret.getValue();
             console.log('云函数',returnValue);
          });
     * */

    return formBindingData.createFormBindingData(formData);
  }

  onCastToNormalForm(formId) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId) {
    // Called to notify the form provider to update a specified form.
  }

  onChangeFormVisibility(newStatus) {
    // Called when the form provider receives form events from the system.
  }

  onFormEvent(formId, message) {
    // Called when a specified message event defined by the form provider is triggered.
  }

  onRemoveForm(formId) {
    // Called to notify the form provider that a specified form has been destroyed.
  }

  onAcquireFormState(want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};